package com.olympia.myapp.custom;

import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.Spinner;

import com.olympia.myapp.R;

public class Dialog_AddMoney extends Dialog implements OnClickListener {
	Activity activity;
	ProcessDialogAddMoney process;
	EditText edtCardNum;
	EditText edtCardSeri;
	Spinner spiner;
	String[] telco = null;

	Button btnOk;
	ImageView btnClose;
	public ProgressBar progress;
	int typeCard = 0;// 0 viettel, 1 mobil, 2 vina, 3 beeline, 4 vietnammobile

	/**
	 * 
	 * @param context
	 */
	public Dialog_AddMoney(Context context, ProcessDialogAddMoney process) {
		super(context);
		activity = (Activity) context;
		/** 'Window.FEATURE_NO_TITLE' - Used to hide the title */
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		/** Design the dialog in main.xml file */
		setContentView(R.layout.dialog_addmoney);
		this.process = process;

		progress = (ProgressBar) findViewById(R.id.progress_dg);
		btnOk = (Button) findViewById(R.id.dg_btnAccept);
		btnClose = (ImageView) findViewById(R.id.dg_iconClose);
		edtCardNum = (EditText) findViewById(R.id.edt_cardnumber);
		edtCardSeri = (EditText) findViewById(R.id.edt_cardSeri);
		btnOk.setOnClickListener(this);
		btnClose.setOnClickListener(this);

		telco = context.getResources().getStringArray(R.array.data_telco);
		spiner = (Spinner) findViewById(R.id.spn_cardType);
		spiner.setOnItemSelectedListener(new OnItemSelectedListener() {
			@Override
			public void onItemSelected(AdapterView<?> arg0, View arg1, int pos,
					long arg3) {
				typeCard = pos;
			}

			@Override
			public void onNothingSelected(AdapterView<?> arg0) {
			}
		});
		ArrayAdapter<String> adapter = new ArrayAdapter<String>(context,
				android.R.layout.simple_spinner_item, telco);
		adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
		spiner.setAdapter(adapter);
	}

	@Override
	public void onClick(View v) {
		if (v == btnClose) {
			dismiss();
		} else if (v == btnOk) {
			String num = edtCardNum.getText().toString();
			String seri = edtCardSeri.getText().toString();
			if (validate(num, seri, typeCard)) {
				progress.setVisibility(View.VISIBLE);
				process.click_Ok(num, seri, typeCard);
			}
		}
	}

	private boolean validate(String num, String seri, int type) {
		if (num.length() <= 8) {
			edtCardNum.setError("Mã số thẻ cào không đúng");
			return false;
		}
		if (seri.length() <= 8) {
			edtCardSeri.setError("Số Seri thẻ cào không đúng");
			return false;
		}
		if (type == -1) {
			// edtCardNum.setError("Mã số thẻ cào không hợp lệ");
			return false;
		}
		return true;
	}

	public static abstract class ProcessDialogAddMoney {
		public abstract void click_Ok(String cardNum, String cardSeri,
				int typeCard);
	}
}
